package com.junxonline.plat.controller.floorplan;

import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.floorplan.FloorPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName: FloorPlanController
 * @Description: 户型控制器
 * @author JunX
 */

@RestController
@RequestMapping(value = "/admin/floorplan")
public class FloorPlanController extends BaseController {

    @Autowired
    private FloorPlanService floorPlanService;

    /**
     * 获取户型数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getFloorPlan")
    public ResultVO getFloorPlan(@RequestBody QueryFloorPlanVO vo) {

        if (CommonUtils.isEmpty(vo.getHouseFloorPlanId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(floorPlanService.getFloorPlan(vo));
    }

    /**
     * 筛选查询户型数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryFloorPlan")
    public PageResultVO queryFloorPlan(PageVO page, QueryFloorPlanVO vo) {
        return getReturnSuccess(floorPlanService.queryFloorPlan(page, vo));
    }


    /**
     * 添加户型
     * @param houseFloorPlanFile
     * @param houseFloorPlanName
     * @return
     */
    @RequestMapping(value = "/addFloorPlan")
    public ResultVO addFloorPlan(@RequestParam MultipartFile houseFloorPlanFile, @RequestParam String houseFloorPlanName) {

        if (CommonUtils.isEmpty(houseFloorPlanFile) || CommonUtils.isEmpty(houseFloorPlanName)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        FloorPlanVO vo = new FloorPlanVO();
        vo.setHouseFloorPlanName(houseFloorPlanName);
        vo.setHouseFloorPlanFile(houseFloorPlanFile);

        boolean success = floorPlanService.addFloorPlan(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改户型
     * @param houseFloorPlanFile
     * @param houseFloorPlanName
     * @return
     */
    @RequestMapping(value = "/editFloorPlan")
    public ResultVO editFloorPlan(@RequestParam MultipartFile houseFloorPlanFile, @RequestParam String houseFloorPlanName, @RequestParam Long houseFloorPlanId) {

        if (CommonUtils.isEmpty(houseFloorPlanId) || CommonUtils.isEmpty(houseFloorPlanName)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        FloorPlanVO vo = new FloorPlanVO();
        vo.setHouseFloorPlanId(houseFloorPlanId);
        vo.setHouseFloorPlanName(houseFloorPlanName);
        vo.setHouseFloorPlanFile(houseFloorPlanFile);

        boolean success = floorPlanService.editFloorPlan(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除户型
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delFloorPlan")
    public ResultVO delFloorPlan(@RequestBody FloorPlanIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] floorPlanIdsArray = vo.getFloorPlanIds();

        boolean successMenu = floorPlanService.delFloorPlan(floorPlanIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }
    
}


